<template>
	<view class="page flex flex-direction">
		<cu-custom :isBack="true" class="bg-blue">
			<block slot="backText">返回</block>
			<block slot="content">抄表项-{{danger.meterReadingType}}</block>
		</cu-custom>
		<view class="flex-sub" style="overflow: scroll;">

			<view class="padding text-lg text-bold">
				{{danger.checkContent}}
			</view>
			<!-- 单选 -->
			<radio-group class="block" @change="onRadioChooseChange" v-if="danger.meterReadingType=='单选'">
				<view class="cu-form-group" v-for="(item,index) in chooseArray" :key="index">
					<view class="title">{{item.title}}</view>
					<radio class="blue" :class="item.select?'checked':''" :checked="item.select?true:false" :value="item.title"></radio>
				</view>
			</radio-group>
			<!-- 多选 -->
			<checkbox-group class="block" @change="onCheckboxChange" v-if="danger.meterReadingType=='多选'">
				<view class="cu-form-group" v-for="(item,index) in chooseArray" :key="index">
					<view class="title">{{item.title}}</view>
					<checkbox class="blue" :class="item.select?'checked':''" :checked="item.select?true:false" :value="item.title"></checkbox>
				</view>
			</checkbox-group>
			<!-- 数值 -->
			<view class="" v-if="danger.meterReadingType=='数值'">
				<view class="cu-form-group">
					<input type="digit" v-model="danger.meterContent" :placeholder="'请输入范围值：'+danger.meterLowerLimit+'~'+danger.meterUpperLimit">
				</view>
			</view>
			<!-- 文本 -->
			<view class="" v-if="danger.meterReadingType=='文字'">
				<view class="cu-form-group">
					<textarea v-model="danger.meterContent" maxlength="-1" placeholder="请输入文字" style="height: 150px;" />
					</view>
			</view>
		</view>
		<view class="bg-white flex flex-direction padding">
			<button class="cu-btn bg-blue lg" @tap="clickSubmit">确定</button>
		</view>
	</view>
</template>

<script>
	
	import ui from '../../util/ui.js'
	
	let that = null;
	export default {
		data() {
			return {
				danger: {},
				chooseArray: []
			}
		},
		onLoad(options) {
			that = this;
			let danger = JSON.parse(decodeURIComponent(options.danger));
			that.danger = danger;
			// 处理单选 、多选
			{
				let realArray = that.danger.meterContent.split(',');
				let list = that.danger.meterOptions.split(',');
				for (let i = 0; i < list.length; i++) {
					let select = {
						title: list[i],
						select: false
					};
					for (let j = 0; j < realArray.length; j++) {
						if(realArray[j] == list[i]){
							select.select = true;
							break;
						}
					}
					that.chooseArray.push(select);
				}
			}
		},
		methods: {
			/**
			 * 监听选择事件的变化
			 * @param {Object} e
			 */
			onRadioChooseChange: function(e) {
				let value = e.detail.value;
				that.danger.meterContent = value;
			},
			/**
			 * 监听checkbox变化
			 * @param {Object} e
			 */
			onCheckboxChange: function(e) {
				let items = e.detail.value;
				for (let i = 0; i < that.chooseArray.length; i++) {
					let select = that.chooseArray[i];
					select.select = false;
					for (let j = 0; j < items.length; j++) {
						if (items[j] == select.title) {
							select.select = true;
							break;
						}
					}
				}
				that.danger.meterContent = items.join(',');
			},
			/**
			 * 点击确定
			 */
			clickSubmit:function(){
				// 验证数值时是否超过
				// todo
				// 通知上个页面更新danger的信息
				uni.$emit('updateDeviceDanger',that.danger);
				// 返回上一页
				uni.navigateBack()
			}


		}
	}
</script>

<style>
</style>
